package com.jiantong.lawEnforcing.impl.service;

import com.google.common.base.Throwables;
import com.jiantong.lawEnforcing.impl.dao.BreedLegalGroundDao;
import com.jiantong.lawEnforcing.model.BreedLegalGround;
import com.jiantong.lawEnforcing.service.BreedLegalGroundService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.PageInfo;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 案件处罚对照表
 * Created by mz03 on 2017/9/27.
 */
@Slf4j
@Service
@RpcProvider
public class BreedLegalGroundImpl implements BreedLegalGroundService {


    private final BreedLegalGroundDao breedLegalGroundDao;

    @Autowired
    public BreedLegalGroundImpl(BreedLegalGroundDao breedLegalGroundDao){
        this.breedLegalGroundDao = breedLegalGroundDao;
    }


    /**
     * id 查找
     *
     * @param id
     * @return News
     */
    @Override
    public Response<BreedLegalGround> finById(Long id) {
        try {
            BreedLegalGround borrowRecord = breedLegalGroundDao.findById(id);
            if (borrowRecord != null) {
                return Response.ok(borrowRecord);
            } else {
                log.error("BreedLegalGround (id={}) is not found", id);
                return Response.fail("BreedLegalGround.not.found");
            }
        }catch (Exception e){
            log.error("failed to find BreedLegalGround(id={}),cause:{}",id, Throwables.getStackTraceAsString(e));
            return Response.fail("BreedLegalGround.find.fail");
        }
    }

    /**
     * 创建
     *
     * @param breedLegalGround 单据对象
     * @return 主键 id
     */
    @Override
    public Response<Long> create(BreedLegalGround breedLegalGround) {
        try {
            breedLegalGroundDao.create(breedLegalGround);
            return Response.ok(breedLegalGround.getId());
        }catch (Exception e) {
            log.error("failed.to.create.breedLegalGround:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("create.breedLegalGround.failed");
        }
    }

    /**
     * 更新
     *
     * @param breedLegalGround 单据对象
     * @return true or false
     */
    @Override
    public Response<Boolean> update(BreedLegalGround breedLegalGround) {
        try{
            return Response.ok(breedLegalGroundDao.update(breedLegalGround));
        }catch (Exception e){
            log.error("update.failed.breedLegalGround error :{}",Throwables.getStackTraceAsString(e));
            return Response.fail("update.failed.breedLegalGround");
        }
    }

    /**
     * 根据条件分页查询
     *
     * @param pageNo
     * @param pageSize
     * @param map
     * @return
     */
    @Override
    public Response<Paging<BreedLegalGround>> paging(Integer pageNo, Integer pageSize, Map<String, Object> map) {
        try{
            PageInfo pageInfo = new PageInfo(pageNo,pageSize);
            return Response.ok(breedLegalGroundDao.pagingList(pageInfo.getOffset(),pageInfo.getLimit(),map));
        }catch (Exception e){
            log.error("failed to find BreedLegalGround list cause:{}",  Throwables.getStackTraceAsString(e));
            return Response.fail("BreedLegalGround.list.find.fail");
        }
    }

    /**
     * 查询一级类型的列表, 根据一级类型去重查询一个list
     *
     * @return
     */
    @Override
    public Response<List<BreedLegalGround>> findByType() {
        try {
            List<BreedLegalGround> list = breedLegalGroundDao.findByType();
            return Response.ok(list);
        } catch (Exception e) {
            log.error("failed to find BreedLegalGround list cause:{}",  Throwables.getStackTraceAsString(e));
            return Response.fail("BreedLegalGround.list.find.fail");
        }
    }
}
